import request from '../utils/request'
// api/index.js
import axios from 'axios';

const requestlh = axios.create({
    baseURL: 'http://localhost:9977',
});

//上传图片
export const postImg = (data) => {
    return requestlh.post("/receipes/postImg", data).then(response => {
        // 提取data字段中的图片URL
        return response.data.data;
    });
};

//...... 各个axios后台调用

//登录接口
// api.js
export const getLogin=(info)=>{
    return request.post("user/login",info)
}
//根据token查询用户
export const getUserInfo = (info) =>{
    return request.get("user/getUserInfo")
}


//案例
export const getShowNewsDetail = (hid)=>{
    return request({
        method: 'post',
        url: 'portal/showHeadlineDetail',
        data: `hid=${hid}`
    })
}
//注册
export const getRegister=(info)=>{
    return request.post("user/register",info)
}

//delete 例子
export const deleteNewsByHid = (hid)=>{
    return request({
        method: 'delete',
        url: 'headline/removeByHid',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        data: `hid=${hid}`
    })
}

//favorite通过id找全部接口
export const getFavoriteById = (favoriteId)=>{
    return request({
        method: 'post',
        url: 'favorite/getFavoriteById',
        data: `favoriteId=${favoriteId}`
    })
}

//favorite直接找到全部
export const getFavoriteAll = ()=>{
    return request({
        method: 'post',
        url: 'favorite/getFavoriteAll',
    })
}

//recommend直接找全部
export const getRecommendRecipeAll = ()=>{
    return request({
        method: 'post',
        url: 'favorite/getRecommendRecipeAll'
    })
}

//cuisine找全部
export const getCuisineCategoriesAll = ()=>{
    return request({
        method: 'post',
        url: 'favorite/getCuisineCategoriesAll'
    })
}

//recipes找全部
export const getRecipesAll = ()=>{
    return request({
        method: 'post',
        url: 'favorite/getRecipesAll'
    })
}

//user找全部
export const getUserAll = ()=>{
    return request({
        method: 'post',
        url: 'favorite/getUserAll'
    })
}
// 获取所有菜谱分类（包含大分类和小分类）
export const getMainCategories = () => {
  return request({
    method: 'get',
    url: '/category/getAllCategories'
  });
};

// 根据分类ID获取菜谱列表
export const getRecipesByCategory = (categoryId) => {
  return request({
    method: 'get',
    url: '/category/getRecipesByCategory',
    params: { categoryId } // 使用params传递参数
  });
};
//根据Id找菜单，用于菜单展示
export const getRecipesById = (id)=>{
    return request({
        method: 'post',
        url: 'receipes/recipesById',
        data: `id=${id}`
    })
}

//找视频
export const getFavoriteVideosAll = ()=>{
    return request({
        method: 'post',
        url: 'favorite/getFavoriteVideosAll'
    })
}
//根据菜谱Id删除收藏
export const deleteFavoriteById = (recipeId)=>{
    return request({
        method: 'post',
        url: 'favorite/deleteFavorite',
        data: `recipeId=${recipeId}`
    })
}

//添加收藏
export const insertFavoriteAll = (favorites)=>{
    return request.post("favorite/insertFavoriteAll", favorites)
}

//根据user_id查找收藏里的菜谱Id
export const selectRecipeId = (userId)=>{
    return request.get("favorite/selectFavoriteByUserId",{params:{userId}})
}

// 添加菜谱
export const addRecipe = (data, categoryId)=>{
    return request({
        method: 'post',
        url: 'receipes/addRecipe',
        params: { categoryId }, // 将categoryId作为查询参数传递
        data
    })
}

// 搜索菜谱接口
export const searchRecipes = (keyword) => {
    return request({
        method: 'get',
        url: '/category/search',
        params: { keyword }
    });
};

// 获取所有菜谱分类（用于动态分类筛选）
export const getAllRecipeCategories = () => {
    return request({
        method: 'get',
        url: '/category/getAllCategories'
    });
};
